package com.vividsolutions.jts.addons.simulate;

import java.math.BigDecimal;

public class DecimalFormat {

	int maxDigits;

	public DecimalFormat(int maxDigits) {
		this.maxDigits = maxDigits;
	}

	public String format(double d) {
		double ret = roundToSignificantFigures(d, maxDigits);
		return new BigDecimal(ret).toString();
	}

	public static double roundToSignificantFigures(double num, int n) {
		if (num == 0) {
			return 0;
		}

		final double d = Math.ceil(Math.log10(num < 0 ? -num : num));
		final int power = n - (int) d;

		final double magnitude = Math.pow(10, power);
		final long shifted = Math.round(num * magnitude);
		return shifted / magnitude;
	}

}
